package cn.huntercat.decode.controller;

import cn.huntercat.decode.form.Form;
import cn.huntercat.decode.util.SimEncryptUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 功能描述:
 *
 * @Author GuJunBin
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class IndexController {

    @PostMapping("/decode")
    public void decode(@RequestBody Form form, HttpServletResponse response) throws IOException {
        if (form.getYear().length() < 2) {
            form.setYear(0 + form.getYear());
        }
        if (form.getMonth().length() < 2) {
            form.setMonth(0 + form.getMonth());
        }
        if (form.getDay().length() < 2) {
            form.setDay(0 + form.getDay());
        }
        String s = form.getCode().replace("-", "") + form.getYear() + form.getMonth() + form.getDay();
        String enc = SimEncryptUtil.encode(s);
        String decode = SimEncryptUtil.decode(enc);

        JSONObject j = new JSONObject();
        j.put("msg","success");
        j.put("code", "200");
        j.put("data", enc);
        PrintWriter out = response.getWriter();
        out.write(j.toString());
    }
}
